package com.gai.shop.service.impl;

import com.gai.shop.dao.ProductDao;
import com.gai.shop.dao.impl.ProductDaoImpl;
import com.gai.shop.entity.Page;
import com.gai.shop.entity.Product;
import com.gai.shop.service.ProductService;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    private final ProductDao PD = new ProductDaoImpl();

    /**
     * 使用类别id获取商品数据的总行数
     * @param tid
     * @return
     */
    @Override
    public Integer selectTotalData(Integer tid) {
        Integer result = 0;
        try {
            if (tid>0) {
                result = PD.selectTotalData(tid).intValue();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *
     * @param tid
     * @param page
     * @return
     */
    @Override
    public List<Product> selectProductLimitByTid(Integer tid, Page page) {
        List<Product> productList = null;
        try {
            if (page != null && tid > 0) {
                productList = PD.selectProductLimitByTid(tid,page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return productList;
    }

    /**
     *
     * @param pid
     * @return
     */
    @Override
    public Product selectProductByPid(Integer pid) {
        Product product = null;
        try {
            if (pid>0) {
                product = PD.selectProductByPid(pid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return product;
    }

    /**
     * 商品数据的动态查询
     * @param pname
     * @param ptime
     * @return
     */
    @Override
    public List<Product> getProListByKey(String pname, String ptime) {
        List<Product> productList = null;
        try {
            productList = PD.getProListByKey(pname, ptime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return productList;
    }

    @Override
    public Integer addPro(Product product) {
        Integer result = 0;
        try {
            if (product != null) {
                result = PD.addPro(product);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
